package _11_整理题目._3_字符串_递归_dfs_bfs_双指针._子串子序列_最值;

import org.junit.Test;

import java.util.LinkedList;
import java.util.Queue;

/**
 * https://leetcode-cn.com/problems/find-k-length-substrings-with-no-repeated-characters/
 *
 * 给你一个字符串 S，找出所有长度为 K 且不含重复字符的子串，请你返回全部满足要求的子串的 数目。
 *
 */
public class _012_长度为k的无重复子串 {

    public int numKLenSubstrNoRepeats(String s, int k) {
        int n = s.length();
        if(k > n) return 0;
        Queue<Character> queue = new LinkedList<>();
        int total = 0;
        for(int i=0; i<n; i++) {
            while(queue.contains(s.charAt(i))) {
                queue.poll();
            }
            queue.offer(s.charAt(i));
            if(queue.size() == k) {
                System.out.println(queue);
                total++;
                queue.poll();
            }
        }
        return total;
    }

    @Test
    public void main() {
        String s = "havefunonleetcode";
        int k = 5;
        System.out.println(numKLenSubstrNoRepeats(s, k));
    }
}
